<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ include file="../taglibs.jsp" %>

<s:set id="currentUser" value="#session.currentUser" />
<s:set id="systemConfigs" value="#session.systemConfig" />

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><s:text name="FILE.label.title"/></title>
<link rel="stylesheet" type="text/css" href="../../css/style.css" />
<link rel="stylesheet" type="text/css" href="../../scripts/ext/resources/css/ext-all.css" />
<script type="text/javascript" src="../../scripts/ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="../../scripts/ext/ext-all.js"></script>
<script type="text/javascript" src="../../scripts/ext/locale/ext-lang-${locale}.js"></script>
<script type="text/javascript" src="../../scripts/ext/plugins/dataview/user-view-plugins.js"></script>
<script type="text/javascript">
<!--

Ext.BLANK_IMAGE_URL = '../../scripts/ext/resources/images/default/s.gif';

Ext.namespace('HaiChuan');

Ext.onReady(function(){

    Ext.QuickTips.init();

    HaiChuan.webroot = '${webroot}';

    HaiChuan.isSearch = '${isSearch}';

    HaiChuan.recordsPerPage = parseInt('${systemConfigs.recordsPerPage}') || 10;

    HaiChuan.storeUser = new Ext.data.JsonStore({
        url: HaiChuan.webroot + '/ajax/user/listUser.do',
        baseParams: {},
        totalProperty: 'totalCount',
        root: 'items',
        id: 'id',
        fields: [
            'id', 'name', 'loginName', 'email',
            {name:'balance', type:'float'},
            {name:'submitTime', type:'string', dateFormat:'timestamp'},
            {name:'status', type: 'int'}
        ]
    });

    HaiChuan.gridUser = new Ext.grid.GridPanel({
        id: 'list-view',
        store: HaiChuan.storeUser,
        autoHeight: true,
        loadMask: true,
        stripeRows: true,
        bodyStyle: 'width:100%',
        viewConfig: {
            forceFit: true
        },
        title: '用户列表',
        autoExpandColumn: 'name',

        columns:[{
            id: 'name',
            header: '用户名称',
            dataIndex: 'name',
            width: 100,
            renderer: function(value, metaData, record){
                return String.format('{0}', value);
            },
            sortable: true
        },{
            header: '登录名称',
            dataIndex: 'loginName',
            width: 100,
            sortable: false
        },{
            header: '电子邮件',
            dataIndex: 'email',
            width: 60,
            sortable: true
        },{
            header: '费用余额',
            dataIndex: 'balance',
            width: 40,
            renderer: function(value, metaData, record){
                return String.format('{0}元', value);
            },
            sortable: true
        },{
            header: '注册时间',
            dataIndex: 'submitTime',
            width: 60,
            sortable: true
        },{
            id: 'status',
            header: '使用状态',
            dataIndex: 'status',
            width: 40,
            renderer: function(value, metaData, record){
                return String.format('<a href="javascript:HaiChuan.changeStatus(\'{1}\', {2});">{0}</a>', value==0?'未开通':'已开通', record.id, value);
            },
            sortable: true
        },{
            header: '操作',
            width: 60,
            renderer: function(value, metaData, record){
                return String.format('<a href="javascript:HaiChuan.charge(\'{0}\', \'{1}\', {2});">充值/扣费</a>', record.id, record.data.name, record.data.balance);
            }
        }],

        tbar: [
            '用户名称: ',
            {
                xtype: 'textfield',
                id: 'userName',
                name: 'userName',
                width: 160
            },
            ' ',
            '登录名称: ',
            {
                xtype: 'textfield',
                id: 'loginName',
                name: 'loginName',
                width: 160
            },
            ' ',
            '使用状态: ',
            {
                xtype: 'combo',
                width: 100,
                hiddenId: 'status',
                hiddenName: 'status',
                triggerAction: 'all',
                mode: 'local',
                store: [[-1, '全部'], [0, '未开通'], [1, '已开通']],
                value: -1,
                editable: false,
                listClass: 'x-combo-list-small'
            },
            ' ',
            {
               xtype: 'button',
               text: '查询',
               icon: '../../images/find.png',
               enableToggle: true,
               handler: function(){
                   var o = {
                       isSearch: 'true',
                       userName: Ext.get('userName').getValue(),
                       loginName: Ext.get('loginName').getValue(),
                       status: Ext.get('status').getValue(),
                       start: 0, limit: HaiChuan.recordsPerPage
                   };
                   HaiChuan.storeUser.baseParams = HaiChuan.storeUser.baseParams || {};
                   HaiChuan.storeUser.reload({params:o});
               }
            }
        ]
    });

    HaiChuan.pagingToolbarUser = new Ext.PagingToolbar({
        pageSize: HaiChuan.recordsPerPage,
        width: 440,
        store: HaiChuan.storeUser,
        displayInfo: true,
        displayMsg: '<s:text name="FILE.label.document.pagingToolbar.displayMsg"/>',
        emptyMsg: '<s:text name="FILE.label.document.pagingToolbar.emptyMsg"/>'
    });

    HaiChuan.panelUser = new Ext.Panel({
        id: 'user-view',
        frame: false,
        autoHeight: true,
        layout: 'card',
        activeItem: 0,
        items: [HaiChuan.gridUser],
        bbar: HaiChuan.pagingToolbarUser
    });

    (function(){
        HaiChuan.storeUser.load({params: {start:0, limit:HaiChuan.recordsPerPage}});
    }).defer(100);

    (function(){
        HaiChuan.panelUser.setWidth(Ext.lib.Dom.getViewWidth(true)-10);
        HaiChuan.panelUser.render('user-div');
    }).defer(0);

    HaiChuan.changeStatus = function(userId, status) {
        if (status == 0) status = 1;
        else
        if (status == 1) status = 0;
        Ext.Ajax.request({
            url: HaiChuan.webroot + '/ajax/user/changeStatus.do',
            params: { usrId: userId, status: status },
            success: function(response, options){
                if (Ext.isEmpty(response.responseText)) {
                    return;
                }
                var result = Ext.decode(response.responseText);
                if (!Ext.isEmpty(result.success) && result.success) {
                    HaiChuan.storeUser.reload();
                } else if (!Ext.isEmpty(result.msg)) {
                    alert(result.msg);
                }
            },
            failure: function(response, options){
                ;
            }
        });
    }

    HaiChuan.charge = function(userId, userName, balance) {
        var msg = '给用户“' + userName + '”充值/扣费，余额为 ' + balance + '，请输入充值/扣费金额（-为扣费）:';
        Ext.Msg.prompt('充值/扣费', msg, function(btn, text){
            if (btn == 'ok'){
                var diff = Ext.num(text);
                if (!Ext.isNumber(diff)) {
                    Ext.Msg.alert('', '输入不合法。');
                    return;
                }
                Ext.Ajax.request({
                    url: HaiChuan.webroot + '/ajax/user/charge.do',
                    params: { usrId: userId, diff: diff },
                    success: function(response, options){
                        if (Ext.isEmpty(response.responseText)) {
                            return;
                        }
                        var result = Ext.decode(response.responseText);
                        if (!Ext.isEmpty(result.success) && result.success) {
                            HaiChuan.storeUser.reload();
                        } else if (!Ext.isEmpty(result.msg)) {
                            alert(result.msg);
                        }
                    },
                    failure: function(response, options){
                        ;
                    }
                });
            }
        });
    }
});

//-->
</script>
</head>
<body>

  <hr color="#dcebff" class="listmargin">
  <div id="user-div" class="listmargin"></div>
  <hr color="#dcebff" class="listmargin">

</body>
</html>
